package com.ibeeking.found.content.b.rest.service.impl;

import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.content.b.rest.service.IArticleGroupService;
import com.ibeeking.found.content.b.rest.service.IArticleService;
import com.ibeeking.found.content.service.common.vo.ArticleCount;
import com.ibeeking.found.content.service.mapper.ArticleGroupMapper;
import com.ibeeking.found.content.service.common.param.ArticleGroupParam;
import com.ibeeking.found.content.service.common.vo.ArticleGroupVO;
import com.ibeeking.found.content.service.common.dos.ArticleGroupDO;
import com.ibeeking.found.content.service.common.query.ArticleGroupQuery;
import com.ibeeking.found.content.service.common.query.ArticleGroupPageQuery;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.ParamCheckUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ArticleGroupServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-02-18 14:14
 **/
@Service
public class ArticleGroupServiceImpl extends ServiceImpl<ArticleGroupMapper, ArticleGroupDO> implements IArticleGroupService {

    @Resource
    private ArticleGroupMapper articleGroupMapper;
    @Resource
    private IArticleService articleServiceImpl;

    @Override
    public List<ArticleGroupVO> list(ArticleGroupQuery articleGroupQuery) {
        List<ArticleGroupDO> articleGroupDOs = articleGroupMapper.selectList(new LambdaQueryWrapper<ArticleGroupDO>()
                .eq(articleGroupQuery.getId() != null, ArticleGroupDO::getId, articleGroupQuery.getId())
                .in(Collections3.isNotEmpty(articleGroupQuery.getIds()), ArticleGroupDO::getId, articleGroupQuery.getIds())
                .like(StringUtils.isNotBlank(articleGroupQuery.getName()), ArticleGroupDO::getName, articleGroupQuery.getName())
                .orderByDesc(ArticleGroupDO::getCreateTime)
        );
        return JsonUtils.jsonToList(articleGroupDOs, ArticleGroupVO.class);
    }

    @Override
    public Page<ArticleGroupVO> pageList(ArticleGroupPageQuery articleGroupPageQuery) {
        Page<ArticleGroupDO> pages = articleGroupMapper.selectPage(new Page<ArticleGroupDO>(articleGroupPageQuery.getPageNum(), articleGroupPageQuery.getPageSize()), new LambdaQueryWrapper<ArticleGroupDO>().like(StringUtils.isNotBlank(articleGroupPageQuery.getName()), ArticleGroupDO::getName, articleGroupPageQuery.getName()));
        Page<ArticleGroupVO> articleGroupVOPage = BeanUtil.convertPage(pages, ArticleGroupVO.class);
        List<ArticleGroupVO> records = articleGroupVOPage.getRecords();
        if (Collections3.isNotEmpty(records)) {
            //查询文章数
            List<ArticleCount> articleCounts = articleServiceImpl.countByGroup(records.stream().map(ArticleGroupVO::getId).collect(Collectors.toList()));
            Map<Long, Integer> articleCountMap = articleCounts.stream().collect(Collectors.toMap(ArticleCount::getId, ArticleCount::getNum));
            records.forEach(x -> x.setArticleNum(articleCountMap.get(x.getId())));
        }
        return articleGroupVOPage;
    }

    @Override
    public Boolean add(ArticleGroupParam articleGroupParam) {
        ParamCheckUtils.stringTrim(articleGroupParam);
        checkUnique(articleGroupParam);
        ArticleGroupDO articleGroupDO = BeanUtil.convertBean(articleGroupParam, ArticleGroupDO.class);
        articleGroupDO.setCreateBy(UserUtils.getUserId());
        articleGroupDO.setCreateTime(LocalDateTime.now());
        articleGroupDO.setModifyTime(LocalDateTime.now());
        return articleGroupMapper.insert(articleGroupDO) > 0;
    }

    @Override
    public Boolean modify(ArticleGroupParam articleGroupParam) {
        ParamCheckUtils.stringTrim(articleGroupParam);
        checkUnique(articleGroupParam);
        ArticleGroupDO articleGroupDO = BeanUtil.convertBean(articleGroupParam, ArticleGroupDO.class);
        articleGroupDO.setModifyBy(UserUtils.getUserId());
        articleGroupDO.setModifyTime(LocalDateTime.now());
        return articleGroupMapper.updateById(articleGroupDO) > 0;
    }

    @Override
    public ArticleGroupVO view(Long id) {
        ArticleGroupDO articleGroupDO = articleGroupMapper.selectById(id);
        return BeanUtil.convertBean(articleGroupDO, ArticleGroupVO.class);
    }

    @Override
    public Boolean del(Long id) {
        List<ArticleCount> articleCounts = articleServiceImpl.countByGroup(Collections3.newArrayList(id));
        DataUtils.trueThrowException(Collections3.isNotEmpty(articleCounts), "当前分组下有文章,禁止删除");
        return articleGroupMapper.deleteById(id) > 0;
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(ArticleGroupParam param) {
        LambdaQueryWrapper<ArticleGroupDO> queryWrapper = new LambdaQueryWrapper<ArticleGroupDO>()
                .ne(param.getId() != null, ArticleGroupDO::getId, param.getId())
                .eq(StringUtils.isNotBlank(param.getName()), ArticleGroupDO::getName, param.getName());
        List<ArticleGroupDO> dos = articleGroupMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在相同名称的分组!");
        }
    }
}
